// 把工作流详情的 instance数据转成 对象

import { useSetState } from "ahooks";
import { useEffect } from "react";

type TnstanceDataItem = {
  caption: string;
  dataType: number;
  name: string;
  value: string;
  valueData: any
};

const objKeys = ["Files", "Details", "Details2"];

const useTransInstance = (instanceData: any[] = []) => {
  const [state, setState] = useSetState<Record<string, TnstanceDataItem>>({});

  useEffect(() => {
    const obj = {};
    instanceData.forEach((item) => {
      const value = item.value;
      if (value && objKeys.includes(item.name)) {
        Reflect.set(item, "valueData", JSON.parse(value));
      }

      Reflect.set(obj, item.name, item);
    });

    setState(obj);
  }, [instanceData]);

  return state;
};

export default useTransInstance;
